import 'package:flutter/material.dart';
import 'package:flutter_lang/lang_index.dart';
import 'package:flutter_lifecycle_base/base_state_widget.dart';
import 'package:provider/provider.dart';
import 'package:route_manager/wx_navigator.dart';
import 'package:wallet_home/model/wallet_list_model.dart';
import 'package:wallet_home/search/pages/search_page_result.dart';
import '../../module_config.dart';
import '../model/search_model.dart';
import 'custom_app_bar.dart';
import 'search_bar.dart';
import 'group_list_view.dart';
import 'search_title_view.dart';
import 'search_item_view.dart';
import '../../widget/coins_manager.dart';

class SearchPage extends BaseStatefulWidget {
  @override
  _SearchPageState createState() => _SearchPageState();
}

class _SearchPageState extends BaseWidgetState<SearchPage> {
  List<CoinsBean> dataSources = [];

  @override
  void initState() {
    super.initState();
    dataSources = CoinManager().allCoinList;
  }

  @override
  Widget build(BuildContext context) {
    dataSources = CoinManager().allCoinList;
    return Scaffold(
        appBar: CustomAppBar(
          context,
          child: GestureDetector(
            child: SearchBar(
              enabled: false,
              hideLeft: false,
              hideRight: true,
              hint: "${WXLang.$t("search_property",package: ModuleConfig.moduleName)}",
              leftButtonClick: () {
                Navigator.pop(context);
              },
              onChanged: (string) {
                print("$string");
              },
            ),
            onTap: () {
              WXNavigator.pushNamed(routeName: '/wallet_search_result');
            },
          ),
        ),
        body: ListView.separated(
          itemCount: dataSources.length,
          itemBuilder: (_, itemIndex) {
            return ListViewItem(dataSources[itemIndex], () {
              CoinsBean coin = dataSources[itemIndex];
              if (coin.isSelected) {
                CoinManager().saveSelectedCoin(coin.title);
              } else {
                CoinManager().removeSelectedCoin(coin.title);
              }
            });
          },
          separatorBuilder: (_, index) {
            return Divider(height: 0.5);
          },
        ));
  }

  @override
  void disappear() {
    super.disappear();
    print("多选页面退出");
    CoinManager().completeCoinSelected();
  }

  @override
  void didAppear() {
    super.didAppear();
    setState(() {
      CoinManager().completeCoinSelected();
    });
  }
}
